# -*- coding: UTF-8 -*- #
"""
-------真正的勇士， 敢于直面惨淡的Warning、 敢于正视淋漓的Error-----
@author : 贾鹏博
@file : 18、Appium 常用设备交互api.py
@time : 2022/6/2 21:57
"""

"""
测试过程中模拟来电
模拟网络切换
运行过程中获取系统日志
截图
"""

import os
import time
from appium import webdriver
from appium.webdriver.extensions.android.gsm import GsmCallActions


class Test_phone_message():

    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = "Android"
        desired_caps['platformVersion'] = '6.0'
        desired_caps['deviceName'] = 'emulator-5554'
        desired_caps['appPackage'] = 'com.xueqiu.android'
        desired_caps['appActivity'] = 'com.xueqiu.android.common.MainActivity'
        desired_caps['noReset'] = 'true'
        # desired_caps['dontStopAppOnReset'] = 'true'
        desired_caps['skipDeviceInitialization'] = 'true'
        desired_caps['unicodeKeyBoard'] = 'true'
        desired_caps['resetKeyBoard'] = 'true'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.quit()

    def test_mobile(self):
        # 模拟打电话
        self.driver.make_gsm_call('13812345678', GsmCallActions.CALL)
        # 模拟发短信
        self.driver.send_sms('13812345678', 'hello appium api')
        # 录屏功能
        self.driver.start_recording_screen()
        # 模拟修改网络设置   1为飞行模式  2为wifi 4为数据流量 6为移动数据和wifi
        self.driver.set_network_connection(1)
        time.sleep(3)
        # 设置回数据流量
        self.driver.set_network_connection(4)
        time.sleep(3)
        # 如果文件不存在，则会创建该文件
        dirs = './photos'
        if not os.path.exists(dirs):
            os.mkdir(dirs)
        # 截图 并保存到 photos 目录下
        self.driver.get_screenshot_as_file('./photos/img.png')
        # 关掉录屏
        self.driver.stop_recording_screen()
